<template>
	<div class="system-user-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入用户名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10"  @click="onOpenAddMusic">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					添加音乐
				</el-button>
			</div>
			<el-table :data="tableData.data" style="width: 80%," stripe>
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="title" label="歌曲名" show-overflow-tooltip></el-table-column>
				<el-table-column prop="author" label="歌手" show-overflow-tooltip></el-table-column>
				<el-table-column prop="album" label="专辑" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column prop="fileUpload.fileSize" label="大小" show-overflow-tooltip :formatter="upfileSize"></el-table-column> -->
				<el-table-column prop="createTime" sortable label="上传时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button  size="small" type="text" @click="handlePlay(scope.row)">播放</el-button>
						<!-- <el-button  size="small" type="text">修改</el-button> -->
						<el-button  size="small" type="text" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="tableData.param.pageNum"
				background
				v-model:page-size="tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<AudioPlayer :audioSrc='musicSrc'/>
		<AddMusic ref="addMusicRef" @initTableData="initTableData" />
		<EditMusic ref="editMusicRef" @initTableData="initTableData" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue';
import { ElMessageBox } from 'element-plus';
import AddMusic from '/@/views/storage/musicLibrary/component/addMusic.vue';
import EditMusic from '/@/views/storage/musicLibrary/component/editMusic.vue';
import AudioPlayer from '/@/components/player/AudioPlayer.vue';
import {blueTMusicApi, fileUploadApi} from '/@/api/storage/index';
import { number } from 'echarts';

const musicApi = blueTMusicApi();
const fileApi = fileUploadApi();

// 定义接口来定义对象的类型
interface TableDataRow {
	musicId: string,
	title: string,
	author: string,
	album: string,
	fileUpload: {
		fileAddress: string,
		fileSize:number,
	}
}
interface TableDataState {
	tableData: {
		data: Array<TableDataRow>;
		total: number;
		loading: boolean;
		param: {
			pageNum: number;
			pageSize: number;
		};
	};
	musicSrc: string;
}

export default defineComponent({
	name: 'musicLibrary',
	components: { AddMusic, EditMusic,AudioPlayer },
	setup() {
		const addMusicRef = ref();
		const editMusicRef = ref();
		const state = reactive<TableDataState>({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
				},
			},
			musicSrc: '',
		});

		// 初始化表格数据
		const initTableData = async () =>  {
			const pageNum = state.tableData.param.pageNum;
			const pageSize = state.tableData.param.pageSize;
			await musicApi.list({pageNum,pageSize}).then(res=>{
				state.tableData.data = res.data.records
				state.tableData.total = res.data.total
			})
		};
		// 打开新增音乐弹窗
		const onOpenAddMusic = () => {
			addMusicRef.value.openDialog();
		};
		// 打开修改音乐弹窗
		const onOpenEditMusic = (row: TableDataRow) => {
			editMusicRef.value.openDialog(row);
		};
		// 播放
		const handlePlay = (row: TableDataRow) => {
			state.musicSrc =row.fileUpload.fileAddress;
		};
		// 删除用户
		const onRowDel = (row: TableDataRow) => {
			ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.title}”，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					musicApi.delete({id:row.musicId});
					initTableData();
				})
				.catch(() => {});
		};
		//文件大小计算
		const upfileSize = (row: TableDataRow) => {
			const fileSize = row.fileUpload.fileSize/1024/1024;
			return fileSize.toFixed(2)+'MB';
		}
		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.pageSize = val;
			initTableData();
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.pageNum = val;
			initTableData();
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			addMusicRef,
			editMusicRef,
			upfileSize,
			handlePlay,
			onRowDel,
			onOpenAddMusic,
			onOpenEditMusic,
			initTableData,
			onHandleSizeChange,
			onHandleCurrentChange,
			...toRefs(state),
		};
	},
});
</script>

<style lang="scss">
.el-row {
  margin-bottom: 20px;
}
.el-row:last-child {
  margin-bottom: 0;
}
.el-col {
  border-radius: 4px;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
</style>